package com.youkeda.yikao.web.control;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class CookieControl {
    @RequestMapping("/cookie/add")
    public String addCookie(HttpServletResponse response){
        Cookie cookie=new Cookie("coloe","red");

        cookie.setPath("/");
        //有效时间
        cookie.setMaxAge(10);
        //域名，顶级域名
        cookie.setDomain("youkeda.com");
        //不允许前端访问
        cookie.setHttpOnly(true);

        response.addCookie(cookie);

        return "写入Cookie";
    }
    @RequestMapping("/cookie/read")
    public String readCookie(@CookieValue("color") String color, HttpServletResponse response, HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        return "读取Cookie"+color;
    }
}
